#include "pwm.h"
#include "lpc_2148.h"
#include "utils.h"
#include "softtimer.h"
#include "uart.h"

#define BUZZER BIT25
#define BUZZER_DIR FIO1DIR
#define BUZZER_SET FIO1SET
#define BUZZER_CLR FIO1CLR
#define BUZZER_TIMEOUT 10

extern void lpc2000_debug_devopen( int(*put)(int) );
extern void lpc2000_debug_printf(char const *format, ...);
extern int init_counter;
extern int interrupt_received;
extern unsigned long buzz_address;

struct uclk buzzer_timer;

static void start_buzzer_timer(void);
static void stop_buzzer_timer(void);

/**
********************************************************************************************
  Function Name :	buzzer_on()
  
  Description   :	
  
  Input         :	
  
  Output        :	Void
  
  Note          :
********************************************************************************************
*/
void buzzer_on()
{    
    lpc2000_debug_devopen(uart0Putch);
    lpc2000_debug_printf("buzzer.c enter buzzer_on counter=%d, interrupts=%d, addr=0x%x\n",
                         init_counter, interrupt_received, buzz_address);
//    delay(100000);
  stop_buzzer_timer();// May be use dereg_uclk directly.
  BUZZER_DIR |= BUZZER;
  BUZZER_CLR |= BUZZER;	
  start_buzzer_timer();
}

/**
********************************************************************************************
 Function Name : turn_off_lcd_back_light()

 Description   :	

 Input         :	

 Output        : Void

 Note          :
********************************************************************************************
*/
void turn_off_buzzer(void)
{
    lpc2000_debug_printf("buzzer.c enter turn_off_buzzer counter=%d\n",init_counter);
//    delay(100000);
   BUZZER_DIR |= BUZZER;
   BUZZER_SET |= BUZZER;
}

/**
********************************************************************************************
 Function Name : start_buzzer_timer()

 Description   :	

 Input         :	

 Output        : Void

 Note          :
********************************************************************************************
*/
static void start_buzzer_timer(void)
{
    lpc2000_debug_printf("buzzer.c enter start_buzzer_timer counter=%d\n",init_counter);
//    delay(100000);
  def_uclk(&buzzer_timer);
  buzzer_timer.repeat = 0;
  buzzer_timer.expired= 0;
  buzzer_timer.ticks = BUZZER_TIMEOUT;
  buzzer_timer.function_callback = turn_off_buzzer;
  reg_uclk(&buzzer_timer);
}

/**
********************************************************************************************
 Function Name : stop_buzzer_timer()

 Description   :	

 Input         :	

 Output        : Void

 Note          :
********************************************************************************************
*/
static void stop_buzzer_timer(void)
{
    lpc2000_debug_printf("buzzer.c enter stop_buzzer_timer counter=%d\n",init_counter);
//    delay(100000);

  dereg_uclk(&buzzer_timer);
}

